<template>
	<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" @down="downCallback()" @up="upCallback">
		<view>
			<view v-for="(item, _index) in dataList" :key="_index">
				<view class="bg-white" style="padding: 0 40rpx;">
					<view class="flex-center" style="padding: 40rpx 0;border-bottom: 1rpx solid #EEEEEE;">
						<view class="u-radius-16 img-container clear-flex">
							<x-image :src="showImage(item.imgUrl, 'logo')"/>
						</view>

						<view class="flex-between flex-column"
							style="padding: 2rpx 0 0 20rpx; height: 178rpx; width: 100%;">
							<view class="u-f-28 u-lh-40 font-medio linecamp-1">{{ item.name || '' }}</view>

							<view class="u-m-t-10 u-f-26 u-lh-36 flex-center">
								<view class="u-m-r-10 color-grew">{{ lang_shop.staffStatus }}:</view>
								<view class="color-red">{{ shelfStatusText(item.staffStatus) }}</view>
							</view>

							<view class="u-f-24 u-lh-34 flex-between flex-column">
								<view></view>

								<view class="flex-center-between u-m-t-14">
									<view></view>

									<view class="flex-center">
										<view class="u-m-r-20">
											<x-common-btn width="128" height="56" color="#333333" background="#FFFFFF"
												borderColor="#e9e9e9" @click="changeAction(item)">
												<view class="u-f-24 u-lh-34 font-medio">
													{{ lang_common[item.staffStatus === 1 ? 'edit' : 'delete'] }}</view>
											</x-common-btn>
										</view>

										<view class="u-m-r-20">
											<x-common-btn width="128" height="56" color="#FD1E43"
												background="rgba(253, 30, 67, 0.15)" borderColor="#FD1E43"
												@click="workInfoAction(item)">
												<view class="u-f-24 u-lh-34 font-medio">{{ lang_shop.works }}</view>
											</x-common-btn>
										</view>

										<x-common-btn width="128" height="56" @click="shelfStaffAction(item, _index)">
											<view class="u-f-24 u-lh-34 font-medio">{{ shelfText(item.staffStatus) }}
											</view>
										</x-common-btn>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</mescroll-uni>
</template>

<script>
	import {
		merchantStaffPage,
		merchantUpOrDownStaff
	} from '@/api/merchant/merchant.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";

	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		props: {
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			}
		},
		data() {
			return {
				dataList: []
			}
		},
		created() {
			uni.$on('update-shop-staff-list', () => {
				this.downCallback()
			})
		},
		methods: {
			shelfStatusText(status = 0) {
				// [上架中，已下架，待上架]
				let arr = {
					['1']: 'onShelf',
					['-1']: 'removeShelf',
					['0']: 'waitShelf'
				}
				return this.lang_common[arr[status]]
			},
			shelfText(status = 0) {
				let arr = {
					['1']: 'offShelf',
					['-1']: 'putShelf',
					['0']: 'putShelf'
				}
				return this.lang_common[arr[status]]
			},
			// 判断是编辑还是删除
			changeAction(item) {
				if (item.staffStatus === 1) {
					this.jumpUrl(`/pages/shop/staff/info/info?merchantStaffId=${ item.id }&type=update`)
				} else {
					this.$emit('delete', item)
				}
			},
			// 上下架员工
			shelfStaffAction(item, index) {
				let targetStatus = item.staffStatus === 1 ? -1 : 1
				merchantUpOrDownStaff({ staffId: item.id, targetStatus }).then(() => {
					this.$set(this.dataList[index], 'staffStatus', targetStatus)
				})
			},
			// 进入员工详情
			workInfoAction(item) {
				uni.navigateTo({
					url: `/pages/shop/staff/worklist/worklist?merchantStaffId=${ item.id }`,
					success: res => {
						res.eventChannel.emit('shop-staff-worklist', item)
					}
				})
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let node = this.$refs['mescrollRef' + this.i]
				merchantStaffPage({
					pageNumber: page.num,
					pageSize: page.size
				}).then(res => {
					node && node.mescroll.endSuccess(res.list.length);
					if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList = this.dataList.concat(res.list); //追加新数据
				}).catch(() => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef' + this.i]
				node && node.mescroll.resetUpScroll()
			},
		}
	}
</script>

<style scoped lang="scss">
	.img-container {
		width: 180rpx;
		height: 180rpx;
		border-radius: 10rpx;
		overflow: hidden;
		background-color: #f9f9f9;
	}
</style>
